<template>
    <div class="modal fade" id="overdueConfigEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="self-modal-dialog" style="width:750px; margin: 0 auto;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" @click="closeThis"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h2 class="modal-title text-center">{{ headTitle }}</h2>
                </div>
                <div class="modal-body" style="padding:15px 30px;">
                    <div class="overdue-top"><span class="base-title">{{title}}</span><i-switch v-model="overdueFlag"><span slot="open">开</span><span slot="close">关</span></i-switch>&nbsp;<b>逾期决策</b></div>
                    <div class="overdue-top-title">{{title}}{{dealOverdueDays}} &nbsp;&nbsp;  首次报备保护时间{{reportOverdueDays}}</div>

                    <div class="decision-content">
                        <div class="decision-content-over" v-if="!overdueFlag"></div>
                        <ul class="decision-ul">
                            <li class="decision-li">
                                <div class="decision-li-title"><i class="required">*</i>决策适用</div>
                                <Select style="width:150px;float:left" v-model="decisionApply">
                                    <Option :value="0" v-show="decisionService == 3 || decisionService == 4" >
                                        全部合作机构
                                    </Option>
                                    <Option :value="1" >合作机构</Option>
                                    <Option :value="2" >合作机构门店</Option>
                                    <Option :value="3" >合作机构组别</Option>
                                    <Option :value="4" >合作机构经纪人</Option>
                                </Select>
                                <div class="decision-warn">
                                    <i class="fa fa-exclamation-circle" aria-hidden="true"></i>
                                    若选择独立合作机构，则同一机构下有多次重复报备情况，同机构其他经纪人都受此影响，其他机构不受影响，依次类推
                                </div>
                            </li>

                            <li class="decision-li" v-for="(d,index) in overdueData">
                                <div class="decision-li-title">
                                    <span v-if="d.decisionAll!=1">
                                        <i class="required">*</i>第{{index+1}}次重复
                                    </span>
                                    <span v-if="d.decisionAll==1">此后所有重复</span>
                                </div>
                                <div class="decision-time">
                                    <Select style="width:150px" v-model="d.decisionType">
                                        <Option :value="1" >{{ changeDesc }}</Option>
                                        <Option :value="2" v-show="decisionTypeShow">无报备保护期</Option>
                                        <Option :value="3" v-show="decisionTypeShow">无法报备</Option>
                                    </Select>
                                </div>
                                <div class="decision-time" v-if="d.decisionType == 1">
                                    <Input style="width: 200px"  v-model="d.decisionTime" />
                                    <Select style="width:150px" v-model="d.decisionTimeType">
                                        <Option :value="5" v-show="decisionTimeTypeShow">无期限</Option>
                                        <Option :value="1" >天</Option>
                                        <Option :value="2" >时</Option>
                                        <Option :value="3" >分钟</Option>
                                    </Select>
                                </div>
                                <a class="decision-trash" v-if="(index == overdueData.length-1 && index !=0)|| (decisionAllShow&&index == overdueData.length-2 )" @click="trashDecision(index)"><i class="fa fa-trash" aria-hidden="true"></i></a>
                            </li>
                        </ul>
                        <div class="decision-action" v-if="!decisionAllShow">
                            <a @click="addOverdue"><span class="decision-action-color">+添加重复次数</span> </a>&nbsp;&nbsp;&nbsp;
                            <a @click="addOverdueAll"><span class="decision-action-color">+此后所有重复</span></a>
                        </div>
                        <div class="decision-reset">
                            <div class="decision-li-title"><i class="required">*</i>重置业务节点</div>
                            <Select style="width:150px" v-model="resetNode">
                                <Option :value="1" >到访成功</Option>
                                <Option :value="2" >认筹成功</Option>
                                <Option :value="3" >认购成功</Option>
                                <Option :value="4" >签约成功</Option>
                                <Option :value="5" >不用重置</Option>
                            </Select>
                            <span>设定报备到达某个阶段后，该重复次数清零</span>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <Button type="primary" @click="saveOverdue">保存配置</Button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>

    export default {
        props: ["premisesInfo"],
        name: "overdue-config",
        data(){
            return{
                overdueData:[],
                decisionAllShow:false,
                resetNode:0,
                overdueFlag:false,
                decisionApply:'',
                decisionService:'',
                premisesId:'',
                title:'',
                dealOverdueDays:0,
                reportOverdueDays:0,
                headTitle: '逾期决策配置',
                changeDesc: '变更报备保护期',
                decisionTypeShow: true,
                decisionTimeTypeShow: true
            }
        },
        methods:{
            getOverdue(type, premisesId, dealOverdueDays, reportOverdueDays){
                var _self = this;
                let body = {};
                _self.type = type;
                _self.decisionService = type;
                _self.premisesId = premisesId;
                if(type == 1){
                    _self.title = '到访延长时间';
                    _self.decisionTypeShow = true;
                    _self.decisionTimeTypeShow = false;
                }else if(type == 2){
                    _self.title = '交易延长时间';
                    _self.decisionTypeShow = true;
                    _self.decisionTimeTypeShow = false;
                } else if(type == 3){
                    _self.title = '报备保护期变更规则';
                    _self.headTitle = '报备保护期高级规则';
                    _self.decisionTypeShow = true;
                    _self.decisionTimeTypeShow = false;
                }else if(type == 4){
                    _self.title = '报备有效期变更规则';
                    _self.changeDesc = '变更报备有效期';
                    _self.headTitle = '报备有效期高级规则';
                    _self.decisionTypeShow = false;
                    _self.decisionTimeTypeShow = true;
                }
                _self.reportOverdueDays = reportOverdueDays;
                _self.dealOverdueDays = dealOverdueDays;
                if(!premisesId){
                    //新增楼盘的时候值可能为空
                    return;
                }
                body.params = {premisesId: premisesId,decisionService:type};
                var url = _self.utilHelper.apiUrl + '/api/getOverdueConfigInfo';
                _self.$http.post(url, body).then((response) => {
                    var res = response.body;
                    if(res.result == 1){
                       _self.overdueData = res.resData.overdueData;
                       if(_self.overdueData){
                            for(var overDue of _self.overdueData){
                                if(overDue && 1 === overDue.decisionAll){
                                    _self.decisionAllShow = true;
                                }
                            }
                       }
                       
                       _self.resetNode = res.resData.decisionReset;
                       _self.overdueFlag = res.resData.overdueSet ==1?true:false;
                       _self.decisionApply = res.resData.decisionFit;
                    }

                });

            },
            saveOverdue(){
              var _self = this;
              let body = {};
              //主要数据 ，是否开启逾期，决策适用，重置业务节点
              let params = {
                  premisesId:_self.premisesId,
                  overdueFlag:_self.overdueFlag == false?0:1,
                  decisionApply:_self.decisionApply,
                  decisionService:_self.decisionService,
                  resetNode:_self.resetNode,
              }
              // 逾期子节点配置，此后逾期配置

              for(let i in _self.overdueData){
                  //设置逾期配置顺序
                  let index = Number(i)+1;
                  _self.overdueData[i].decisionSort = index;
                  _self.overdueData[i].createTime='';
                  _self.overdueData[i].updateTime='';
              }
              params.subNodes = _self.overdueData;
              if(!_self.premisesId){
                  console.log(params);
                  _self.$emit('setOverdueData',_self.decisionService ,params);
                  return;
              }
              body.params = params;
                if(!_self.checkParams()){
                    return;
                }
              //保存请求
                var url = _self.utilHelper.apiUrl + '/api/overdueDecisionConfig';
                _self.$http.post(url, body).then((response) => {
                    var res = response.body;
                    if(res.result == 1){
                        layer.msg('设置成功');
                        _self.closeThis();
                    }else{
                        layer.msg('设置失败');
                    }
                });
            },
            addOverdue(){
                //添加逾期次数
                var _self = this;
                var o = {
                    decisionType:'',
                    decisionTime:'',
                }
                if(!_self.overdueData){
                    _self.overdueData = [];
                }
                _self.overdueData.push(o);
            },
            addOverdueAll(){
                var _self = this;
                var o = {
                    decisionType:'',
                    decisionAll:1,
                }
                if(_self.overdueData.length < 1){
                    layer.msg('请先设置常规逾期配置');
                    return;
                }
                _self.overdueData.push(o);
                _self.decisionAllShow = true;
            },
            trashDecision(index){
                var _self = this;
                for(let o in _self.overdueData){
                    if(o == index){
                        _self.overdueData.splice(index,1);
                    }
                }
                _self.decisionAllShow = false;
            },
            closeThis(){
                var _self = this;
                _self.decisionAllShow = false;
                $("#overdueConfigEdit").modal("hide");
            },
            checkParams(){
                //校验参数
                var _self = this;
                if(!_self.overdueData || _self.overdueData.length == 0){
                    layer.msg('请填写首次重复决策');
                    return false;
                }
                if(!_self.decisionApply && _self.decisionApply !== 0){
                    layer.msg('请选择决策适用范围');
                    return false;
                }
                if(!_self.resetNode){
                    layer.msg("请选择重置业务节点");
                    return false;
                }
                for(var o in _self.overdueData){
                        //校验总设置
                    if(!_self.overdueData[o].decisionType){
                        layer.msg("请设置逾期配置");
                        return false;
                    }
                    if(_self.overdueData[o].decisionType == 1){
                        if(!_self.overdueData[o].decisionTimeType){
                            layer.msg("请选择时间单位");
                            return false;
                        }
                        if(!_self.overdueData[o].decisionTime ){
                            layer.msg("变更报备保护期未填写时间");
                            return false;
                        }
                    }
                    return true;
                }
            }
        }
    }
</script>

<style scoped>
    .base-title{margin-right:30px; }
    .overdue-top-title{color:#ff7900;}
    .decision-li{margin: 10px 0;height: 40px;}
    .decision-li-title{width: 90px;text-align: left;float: left;line-height: 35px;}
    .decision-action{color:#ff7900;margin:10px 0;}
    .decision-ul{margin: 30px 0;}
    .decision-time{float:left;margin-right: 30px;}
    .decision-trash{width: 20px;height: 30px;line-height:30px;color:#FF5E5E;font-size: 18px;}
    .decision-action-color{color:#ff7900;}
    .decision-reset{padding-top: 20px;border-top: 1px solid #ccc;}
    .decision-content{position: relative;}
    .decision-content-over{position: absolute;width: 100%;height: 100%;left: 0;top:0;opacity: 0.8;background: #FFFFFF;z-index:10;}
    .required{color:#FF5E5E;}
    .decision-warn{float:left;width: 350px;margin-left: 30px;}
    .decision-warn i {color:#FF5E5E;font-size: 16px;}
</style>
